<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

<title>Camera Example</title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="css/iconfont.css" rel="stylesheet"/>
    <link rel="stylesheet" href="css/app.css" rel="stylesheet"/>
	<script type="text/javascript" src="js/common.js"></script>
	<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8"/>
</head>

<body>
<button id="btn" type="button" class="mui-btn mui-btn-blue mui-btn-block">录制视频</button>
<script type="text/javascript">
	mui.init();
    document.querySelector('#btn').addEventListener('click',function () {
        // 调用原生android摄像头
        var VIDEOZOOM = 200;
        var MediaStore = plus.android.importClass("android.provider.MediaStore");
        var Intent = plus.android.importClass("android.content.Intent");
        // 导入后可以使用new方法创建类的示例对象
        var intent = new Intent("android.media.action.VIDEO_CAPTURE");
        intent.putExtra("android.intent.extra.videoQuality", 1);//0 means low quality, 1 means high quality
        //intent.putExtra("android.provider.MediaStore.EXTRA_OUTPUT", url);
        intent.putExtra("android.intent.extra.durationLimit", 3);//设置录像时间

        var main = plus.android.runtimeMainActivity();
        main.startActivityForResult(intent,VIDEOZOOM);
        
        // 创建上传任务
		function createUpload(f) {
			var task = plus.uploader.createUpload( "http://demo.tenghoo.net.cn/upload.php", 
				{ method:"POST",blocksize:204800,priority:100 },
				function ( t, status ) {
					alert(t);
					// 上传完成
					if ( status == 200 ) { 
						alert( "Upload success: " + t.url );
					} else {
						alert( "Upload failed: " + status );
					}
				}
			);
			task.addFile( f, {key:"file"} );
			//task.addEventListener( "statechanged", onStateChanged, false );
			task.start();
		}
        
        //获取返回参数
        main.onActivityResult = function(requestCode, resultCode, data) {
            var context = main;
            plus.android.importClass(data);
            var uri = data.getData();
            var resolver = context.getContentResolver();
            plus.android.importClass(resolver);
            var cursor = resolver.query(uri, null, null, null, null);
            plus.android.importClass(cursor);
            cursor.moveToFirst();
            var column = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            // 获取录制的视频路径
            var filePath = cursor.getString(column);

            // 解析视频文件的属性
            plus.io.resolveLocalFileSystemURL(filePath,function(entry) {
                entry.file(function(file){
                    console.log("size=="+file.size); 
                    console.log("name=="+file.name);
                    createUpload(filePath);
                });
            }, function (e) {
                console.log("Resolve file URL failed: " + e.message);
            });
        };
    })
</script>
</body>

</html>